package promote.brozen.base.io.nio;

import promote.brozen.utils.concurrent.Threads;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

/**
 * @author Brozen
 * @date 2020/10/9 8:23 AM
 * @email brozen@qq.com
 */
public class MMap {

    public static void main(String[] args) throws IOException {

        mmapRead();

    }

    private static void mmapRead() throws IOException {
        FileChannel fc = FileChannel.open(Paths.get("/data/logs/error.log"), StandardOpenOption.READ);
        ByteBuffer buffer = ByteBuffer.allocate((int) fc.size());
        fc.read(buffer);

        Threads.silentSleep(1000L);

        buffer.flip();
        CharBuffer cbuffer = CharBuffer.allocate(buffer.limit());
        Charset.forName("UTF-8").newDecoder().decode(buffer, cbuffer, true);

        buffer.flip();
        System.out.println(buffer.getChar());

        cbuffer.flip();
        System.out.println(cbuffer.get());

    }

}
